home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Menus / High Level / MenuBar.cp < prev    next >
Encoding:
Text File  |  1997-06-28  |  2.2 KB  |  125 lines  |  [TEXT/CWIE]

  1. // MenuBar.cp
  2.  
  3. #ifndef MenuBar_h
  4. #include "MenuBar.h"
  5. #endif
  6. #ifndef ListLoop_h
  7. #include "ListLoop.h"
  8. #endif
  9. #ifndef Menu_h
  10. #include "Menu.h"
  11. #endif
  12. #ifndef Tick_h
  13. #include "Tick.h"
  14. #endif
  15. #ifndef UserState_h
  16. #include "UserState.h"
  17. #endif
  18.  
  19. MenuBar::MenuBar()
  20.   : listenToUserState( UserState::The(), *this, &MenuBar::Prepare )
  21.   {
  22.   }
  23.  
  24. MenuBar& MenuBar::The()
  25.   {
  26.     static MenuBar the;
  27.     return the;
  28.   }
  29.  
  30. void MenuBar::Add( ListLink<Menu>& link, AfterEnd )
  31.   {
  32.     MenuBarObject::Add( *link, afterEnd );
  33.     mainMenus.Add( link, afterEnd );
  34.     Invalidate();
  35.   }
  36.  
  37. void MenuBar::Add( ListLink<Menu>& link, AsSecondaryMenu as )
  38.   {
  39.     MenuBarObject::Add( *link, as );
  40.     secondaryMenus.Add( link, afterEnd );
  41.   }
  42.  
  43. void MenuBar::Add( ListLink<Menu>& link,
  44.                          After,
  45.                          const ListLink<Menu>& previous )
  46.   {
  47.     MenuBarObject::Add( *link, after, *previous );
  48.     mainMenus.Add( link, after, previous );
  49.     Invalidate();
  50.   }
  51.  
  52. void MenuBar::Prepare()
  53.   {
  54.     for ( ListLoop<Menu> menu( secondaryMenus );
  55.             menu.Unfinished();
  56.             menu++ )
  57.         menu->Prepare();
  58.  
  59.     bool updateBar = false;
  60.     
  61.     for ( ListLoop<Menu> menu( mainMenus );
  62.             menu.Unfinished();
  63.             menu++ )
  64.       {
  65.         bool wasEnabled = menu->Enabled();
  66.         menu->Prepare();
  67.         if ( menu->Enabled() != wasEnabled )
  68.             updateBar = true;
  69.       }
  70.     
  71.     if ( updateBar )
  72.         DrawMenuBar();
  73.   }
  74.  
  75. Menu& MenuBar::operator[]( MenuID id ) const
  76.   {
  77.     Assert( id != 0 );
  78.     
  79.     for ( ListLoop<Menu> menu( mainMenus );
  80.             menu.Unfinished();
  81.             menu++ )
  82.         if ( menu->ID() == id )
  83.             return *menu;
  84.  
  85.     for ( ListLoop<Menu> menu( secondaryMenus );
  86.             menu.Unfinished();
  87.             menu++ )
  88.         if ( menu->ID() == id )
  89.             return *menu;
  90.     
  91.     Assert( 0 );
  92.     return **mainMenus.First();
  93.   }
  94.  
  95. void MenuBar::Choose( MenuResult target )
  96.   {
  97.     if ( target.Null() )
  98.         return;
  99.     
  100.     Tick stopHighlight( Tick::Now() + 3 );
  101.     
  102.     (*this)[ target.Menu() ].Choose( target.Item() );
  103.     
  104.     // Delay so that the user can see the menu title flash
  105.     stopHighlight.WaitFor();
  106.     
  107.     HighlightNone();
  108.   }
  109.  
  110. void MenuBar::Click( Point p )
  111.   {
  112.     listenToUserState.Flush();
  113.     MenuResult where( MenuBarObject::Click( p ) );
  114.     Choose( where );
  115.   }
  116.  
  117. void MenuBar::Key( ::Key k )
  118.   {
  119.     listenToUserState.Flush();
  120.     Choose( MenuBarObject::Key( k ) );
  121.   }
  122.  
  123. #include "ListOf.cp"
  124. #include "ListLoop.cp"
  125.